package org.example.erp.controller;
import org.example.erp.domain.SysMenu;
import org.example.erp.domain.SysPost;
import org.example.erp.domain.SysRole;
import org.example.erp.domain.SysUser;
import org.example.erp.service.impl.SysMenuServiceImpl;
import org.example.erp.service.impl.SysPostServiceImpl;
import org.example.erp.service.impl.SysRoleServiceImpl;
import org.example.erp.service.impl.SysUserServiceImpl;
import org.example.erp.web.DTO.UserDTO;
import org.example.erp.web.VO.UserPostsRolesVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/api")
public class UserController {
    @Autowired
    private SysUserServiceImpl sysUserServiceImpl;
    @Autowired
    private SysMenuServiceImpl sysMenuServiceImpl;
    @Autowired
    private SysPostServiceImpl postService;
    @Autowired
    private SysRoleServiceImpl roleService;

    // 退出登录
    @GetMapping("/user/logout")
    public DataResult logout() {
        SecurityContextHolder.clearContext();
        return DataResult.success("退出成功");
    }

    // 树形菜单数据
    @GetMapping("/menuManagement/find")
    public DataResult findTreeMenus(@RequestParam String username) {
        List<SysMenu> menus = sysMenuServiceImpl.findMenus(username, "F");
        System.out.println(menus);
        return DataResult.success(menus);
    }

    // 扁平菜单数据
    @GetMapping("/menu/menusList")
    public DataResult findMenuList(@RequestParam String username) {
        return DataResult.success(sysMenuServiceImpl.findMenus(username, null));
    }

    // 查询用户信息
    @GetMapping("/userManagement/find")
    public DataResult findUserByDept(@RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage,
                                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                     @RequestParam(required = false) String username,
                                     @RequestParam(required = false) String phone,
                                     @RequestParam(required = false) String status,
                                     @RequestParam(required = false) String startTime,
                                     @RequestParam(required = false) String endTime,
                                     @RequestParam(required = false) Integer deptId
    ) {
        return DataResult.success(sysUserServiceImpl.findUserByDept(currentPage, pageSize, username, phone, status, startTime, endTime,deptId));
    }

    // 修改用户状态信息
    @PutMapping("/userManagement/updateStatus")
    public DataResult updateStatus(@RequestBody SysUser user) {
        sysUserServiceImpl.updateById(user);
        return DataResult.success();
    }

    //删除用户相关信息
    @DeleteMapping("/userManagement/delete")
    public DataResult deleteUserByIds(@RequestBody List<Integer> userIds) {
        sysUserServiceImpl.deleteUserByIds(userIds);
        return DataResult.success();
    }

    // 查询岗位和角色
    @GetMapping("/userManagement/findPostRole")
    public DataResult findUserPostRoles() {
        List<SysPost> postList = postService.list();
        List<SysRole> roleList = roleService.list();
        UserPostsRolesVO userViewData = new UserPostsRolesVO(postList, roleList);
        return DataResult.success(userViewData);
    }

    //新增用户
    @PostMapping("/userManagement/insert")
    public DataResult insertUser(@RequestBody UserDTO userDTO) {
        return sysUserServiceImpl.insertUser(userDTO);
    }
    // 返回视图
    @GetMapping("/userManagement/findUserById")
    public DataResult findUserById(@RequestParam(value = "userId") Integer userId) {
        return DataResult.success(sysUserServiceImpl.findUserById(userId));
    }
    // 更新用户信息
    @PutMapping("/userManagement/update")
    public DataResult updateUser(@RequestBody UserDTO userDTO){
            sysUserServiceImpl.updateUser(userDTO);
            return DataResult.success();
    }

    @PutMapping("/userManagement/updatePassword")//重置密码
    public DataResult updatePassword(@RequestBody SysUser sysUser){
        boolean b = sysUserServiceImpl.updateById(sysUser);
        if (b) {
            return DataResult.success(b ? "成功":"失败");
        } else {
            return DataResult.error("重置密码失败");
        }
    }
}
